﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlMiM.Common.ViewModel;
using SqlMiM.Common.Interfaces;
using System.Collections.ObjectModel;
using SqlMiM.Common.Framework.Configuration;
using SqlMiM.Common.Framework.Options;
using System.Windows.Input;
using SqlMiM.Common.Commands;
using MvvmFoundation.Wpf;

namespace SqlMiM.ViewModel
{
    public class OptionsViewModel : ObservableObject
    {
        
        private ObservableCollection<IOptionsPage> optionsPages;
        private IOptionsPage activeOptionPage;
        private RelayCommand commitCommand;
        private RelayCommand cancelCommand;
        public event EventHandler CloseRequest;

        public IOptionsPage ActivePage
        {
            get {
                return activeOptionPage; 
            }
            set { 
                activeOptionPage = value;
                this.RaisePropertyChanged("ActivePage");
            }
        }

        public ObservableCollection<IOptionsPage> OptionsPages
        {
            get
            {
                return optionsPages;
            }
            set
            {
                optionsPages = value;
                this.RaisePropertyChanged("OptionsPages");
            }
        }

        public OptionsViewModel()
        {
            OptionsPages = new ObservableCollection<IOptionsPage>();

            // Get Options Dialog Service.
            var options = OptionsService.Current.OptionsPages;

            if (options == null) return;

            // Set new ObservableCollection based on it
            this.OptionsPages = new ObservableCollection<IOptionsPage>();

            foreach (var opt in options)
            {
                opt.Load(ConfigurationService.Current[opt.GetType().Name]);
                this.OptionsPages.Add(opt);
            }

            // Set ActiveOptionPage
            if (this.OptionsPages.Count > 0)
                this.ActivePage = this.OptionsPages[0];

        }


        /// <summary>
        /// Commit all
        /// </summary>
        public ICommand CommitCommand
        {
            get
            {
                if (commitCommand == null)
                {
                    commitCommand = new RelayCommand(this.Commit);

                }
                return commitCommand;
            }
        }

        /// <summary>
        /// Cancel all
        /// </summary>
        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new RelayCommand(this.Cancel);

                }
                return cancelCommand;
            }
        }

        /// <summary>
        /// Call Cancel on all IOptionPage via OptionsService
        /// </summary>
        internal void Cancel()
        {
            OptionsService.Current.Cancel();

            if (this.CloseRequest != null) CloseRequest(this, EventArgs.Empty);
        }

        /// <summary>
        /// Call Commit on all IOptionPage via OptionsService
        /// </summary>
        internal void Commit()
        {
            OptionsService.Current.Commit();

            if (this.CloseRequest != null) CloseRequest(this, EventArgs.Empty);
        }
    }
}
